//
//  SafeAreaContainerViewController.swift
//  Pods
//
//  Created by 肖前欣 on 2025/9/29.
//

import UIKit

class SafeAreaContainerViewController: UIViewController {
    private let contentViewController: UIViewController
    private let navigationBar = UINavigationBar()
    private let titleText: String
    
    init(contentViewController: UIViewController, title: String = "短剧") {
        self.contentViewController = contentViewController
        self.titleText = title
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationBar()
        setupContentViewController()
    }
    
    private func setupNavigationBar() {
        // 设置导航栏
        view.addSubview(navigationBar)
        navigationBar.translatesAutoresizingMaskIntoConstraints = false
        
        // 创建导航项
        let navigationItem = UINavigationItem(title: titleText)
        
        // 创建返回按钮 - 兼容 iOS 12.0
        let backButton: UIBarButtonItem
        if #available(iOS 13.0, *) {
            // iOS 13+ 使用系统图标
            backButton = UIBarButtonItem(
                image: UIImage(systemName: "chevron.left"),
                style: .plain,
                target: self,
                action: #selector(backButtonTapped)
            )
        } else {
            // iOS 12 使用文字或自定义图标
            backButton = UIBarButtonItem(
                title: "< 返回",
                style: .plain,
                target: self,
                action: #selector(backButtonTapped)
            )
        }
        
        // 设置按钮颜色 - 黑色背景下使用白色
        if #available(iOS 13.0, *) {
            backButton.tintColor = .white
        } else {
            backButton.tintColor = .white
        }
        
        navigationItem.leftBarButtonItem = backButton
        
        // 设置导航项
        navigationBar.setItems([navigationItem], animated: false)
        
        // 设置导航栏外观 - 黑色背景
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .black  // 设置为黑色背景
            appearance.titleTextAttributes = [.foregroundColor: UIColor.white]  // 白色标题文字
            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = appearance
        } else {
            navigationBar.barTintColor = .black  // iOS 12 设置为黑色背景
            navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]  // 白色标题文字
        }
        
        // 设置导航栏约束
        NSLayoutConstraint.activate([
            navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }
    
    private func setupContentViewController() {
        // 添加子视图控制器
        addChild(contentViewController)
        view.addSubview(contentViewController.view)
        contentViewController.didMove(toParent: self)
        
        // 设置约束，内容视图在导航栏下方
        contentViewController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            contentViewController.view.topAnchor.constraint(equalTo: navigationBar.bottomAnchor),
            contentViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            contentViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            contentViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
    
    @objc private func backButtonTapped() {
        dismiss(animated: true, completion: nil)
    }
    
    // 可选：支持自定义返回按钮行为
    func setBackButtonAction(_ action: @escaping () -> Void) {
        // 可以在这里添加自定义返回逻辑
    }
    
    // 可选：动态更新标题
    func updateTitle(_ newTitle: String) {
        navigationBar.topItem?.title = newTitle
    }
}
